package com.hung.android;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
	private LinearLayout layout;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		layout  =(LinearLayout)findViewById(R.id.ln);
		createAutoLayout();
		// thay doi svn
		
		//viết riêng 1 phương thức để add giao diện động vào trong linearlayout
	}
	
	public void createAutoLayout(){
		
		for (int i = 0; i < 5; i++) {
			
			LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			
			final View view = inflater.inflate(R.layout.itemlayout, null);
			
			view.setTag(i);// dan tem cho view
			
			// khoi tao cac thanh phan cau tao nen 1 view
			
			TextView tv = (TextView)view.findViewById(R.id.name);
			ImageView img = (ImageView)view.findViewById(R.id.picture);
			
			tv.setText("Tang"+i);
			img.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
				Toast.makeText(MainActivity.this, "so thu tu la"+view.getTag(), Toast.LENGTH_SHORT).show();	
				}
			});
			
			layout.addView(view);
		}
	}

	

}
